package Puzzle;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;

import javax.imageio.ImageIO;

public class Piece {

	private BufferedImage image;
	private int hauteur;
	private int largeur;
	private boolean haut;
	private boolean bas;
	private boolean gauche;
	private boolean droite;
	private int numPiece;
	private int place = 0;

	public boolean isHaut() {
		return haut;
	}

	public void setHaut(boolean haut) {
		this.haut = haut;
	}

	public boolean isBas() {
		return bas;
	}

	public void setBas(boolean bas) {
		this.bas = bas;
	}

	public boolean isGauche() {
		return gauche;
	}

	public void setGauche(boolean gauche) {
		this.gauche = gauche;
	}

	public boolean isDroite() {
		return droite;
	}

	public void setDroite(boolean droite) {
		this.droite = droite;
	}

	public Piece(BufferedImage image, int numeroPiece) {
		super();
		this.image = image;
		this.numPiece = numeroPiece;
	}

	public void CQuellePiece() {
		int[][] res = new int[this.image.getWidth()][this.image.getHeight()];
		int i = 0;
		int j = 0;
		for (i = 0; i < this.image.getWidth(); i++) {
			for (j = 0; j < this.image.getHeight(); j++) {
				Color c = new Color(this.image.getRGB(i, j));
				if (c.getBlue() == 0 && c.getRed() == 0 & c.getGreen() == 0) {
					res[i][j] = 1;
				} else {
					res[i][j] = 0;
				}
			}
		}
		int cptG = 0;
		int cptD= 0;
		int cptH= 0;
		int cptB= 0;
		for (j = 0; j < this.image.getHeight(); j++) {
			if(res[0][j]==0){
				cptG++;
			}
		}
		
		for (j = 0; j < this.image.getHeight(); j++) {
			if(res[this.image.getWidth()-1][j]==0){
				cptD++;
			}
		}
		
		for (i = 0; i < this.image.getWidth(); i++) {
			if(res[i][0]==0){
				cptH++;
			}
		}
		
		for (i = 0; i < this.image.getWidth(); i++) {
			if(res[i][this.image.getHeight()-1]==0){
				cptB++;
			}
		}
		
		if(cptG >= (75*this.image.getHeight())/100){
			this.setGauche(true);
		}
		if(cptD >= (75*this.image.getHeight())/100){
			this.setDroite(true);
		}
		if(cptH >= (75*this.image.getWidth())/100){
			this.setHaut(true);
		}
		if(cptB >= (75*this.image.getWidth())/100){
			this.setBas(true);
		}
//		
//		if(this.gauche==true){
//			System.out.println("Cote gauche !");
//		}
//		if(this.droite==true){
//			System.out.println("Cote Droite !");
//		}
//		if(this.haut==true){
//			System.out.println("Cote Haut !");
//		}
//		if(this.bas==true){
//			System.out.println("Cote Bas !");
//		}

	}

	public void rotationImage() {
		/* Chargement de l'image d'oroigine */
		int max = 0;
		int angle = 0;
		this.hauteur = image.getHeight();
		this.largeur = image.getWidth();
		BufferedImage imageTmp = new BufferedImage(largeur, hauteur,
				BufferedImage.TYPE_INT_RGB);
		BufferedImage imageroter = new BufferedImage(largeur + 500,
				hauteur + 500, BufferedImage.TYPE_INT_RGB);
		BufferedImage imageroter2 = new BufferedImage(largeur + 500,
				hauteur + 500, BufferedImage.TYPE_INT_RGB);
		BufferedImage imageroterSortie = new BufferedImage(largeur + 500,
				hauteur + 500, BufferedImage.TYPE_INT_RGB);
		imageroter.setData(image.getData());

		AffineTransform translation = AffineTransform.getTranslateInstance(
				250, 250);
		AffineTransformOp opTrans = new AffineTransformOp(translation,
				AffineTransformOp.TYPE_BICUBIC);
		opTrans.filter(imageroter, imageroter2);
		imageroterSortie.setData(imageroter2.getData());

		for (int l = 0; l < 90; l++) {
			int cpt;

			Graphics2D graphics = imageTmp.createGraphics();
			graphics.dispose();

			BufferedImage imageroter3 = new BufferedImage(largeur + 500,
					hauteur + 500, BufferedImage.TYPE_INT_RGB);

			AffineTransform rotation = AffineTransform.getRotateInstance(
					Math.toRadians(-1), largeur, hauteur);
			AffineTransformOp op90 = new AffineTransformOp(rotation,
					AffineTransformOp.TYPE_BICUBIC);
			op90.filter(imageroter2, imageroter3);

			imageroter2.setData(imageroter3.getData());

			int i = imageroter2.getWidth() - 1;
			int j = 0;
			Color c = new Color(imageroter2.getRGB(i, j));
			boolean plusegale = true;
			while ((c.getBlue() == 0 && c.getRed() == 0 && c.getGreen() == 0)
					&& (plusegale) && (i > 0)) {
				j = 0;
				c = new Color(imageroter2.getRGB(i, j));
				while ((c.getBlue() == 0 && c.getRed() == 0 && c.getGreen() == 0)
						&& (j < imageroter2.getHeight())) {
					c = new Color(imageroter2.getRGB(i, j));
					j++;

				}
				if (j < imageroter2.getHeight()) {
					plusegale = false;
				}
				i--;
			}
			
			cpt = 0;
			for (int k = 0; k < imageroter2.getHeight(); k++) {
				c = new Color(imageroter2.getRGB(i, k));
				if ((c.getBlue() == 0 && c.getRed() == 0 && c.getGreen() == 0)) {

				} else {
					cpt++;
				}
			}

			if (cpt > max) {
				max = cpt;
				angle = l;
			}

		}

		AffineTransform rotation = AffineTransform.getRotateInstance(
				Math.toRadians(-angle - 1), largeur, hauteur);
		AffineTransformOp op90 = new AffineTransformOp(rotation,
				AffineTransformOp.TYPE_BICUBIC);
		op90.filter(imageroterSortie, imageroter2);
		this.image = imageroter2;
	}

	public int recupererPiece() {
		int[][] res = new int[this.image.getWidth()][this.image.getHeight()];
		int i = 0;
		int j = 0;
		for (i = 0; i < this.image.getWidth(); i++) {
			for (j = 0; j < this.image.getHeight(); j++) {
				Color c = new Color(this.image.getRGB(i, j));
				if (c.getBlue() == 0 && c.getRed() == 0 & c.getGreen() == 0) {
					res[i][j] = 1;
					this.image.setRGB(i, j, 0);
				} else {
					res[i][j] = 0;
				}

			}
		}
		int xMin;
		int xMax;
		int yMin;
		int yMax;
		i = 0;
		j = 0;
		while (res[i][j] == 1) {
			j++;
			if (j == this.image.getHeight() - 1) {
				i++;
				j = 0;
			}
		}

		xMin = i;
		i = this.image.getWidth() - 1;
		j = 0;
		while (res[i][j] == 1) {
			j++;
			if (j == this.image.getHeight() - 1) {
				i--;
				j = 0;
			}
		}
		xMax = i;
		i = 0;
		j = 0;
		while (res[i][j] == 1) {
			i++;
			if (i == this.image.getWidth() - 1) {
				i = 0;
				j++;
			}
		}
		yMin = j;

		i = 0;
		j = this.image.getHeight() - 1;
		while (res[i][j] == 1) {
			i++;
			if (i == this.image.getWidth() - 1) {
				i = 0;
				j--;
			}
		}
		yMax = j;
		this.image = this.image.getSubimage(xMin + 2, yMin + 2,
				xMax - xMin - 3, yMax - yMin - 3);
		return i;

	}

	public void enregistrerPiece() {
		try {
			ImageIO.write(this.image, "png", new File("ressources/piece"
					+ this.numPiece + ".png"));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public int getNumPiece() {
		return this.numPiece;
	}

	public int getPlace() {
		return place;
	}

	public void setPlace(int place) {
		this.place = place;
	}

}
